<template>
  <div>
    <el-container style="background: #f5f7f9;">
      <el-header style="background: #fff;padding: 12px 12px;height:auto;">
        <div>
          <el-form :model="queryData" :inline="true" size="small">
            <el-form-item>
              <el-radio-group v-model="statusRadio" @change="statusChange">
                <el-radio-button label="待处理" />
                <el-radio-button label="完成" />
              </el-radio-group>
            </el-form-item>
            <el-form-item label="批次号">
              <el-input v-model="batchAttribute" type="textarea" :rows="1" style="width: 320px;" placeholder="多个订单号用回车分割，支持批次号、提单号" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="list">搜索</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-header>
      <el-main style="margin-top: 15px; background: #fff;padding: 12px 16px 0 16px;height: 100%;">
        <div style="height: 100%;display: flex;">
          <el-button type="success" size="small" @click="dones">完成</el-button>
        </div>
        <el-table
          ref="table"
          :data="table_body"
          border
          :max-height="tableHeight"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" />
          <el-table-column prop="batchNumber" label="批次号" />
          <el-table-column prop="serviceTypeName" width="300" label="服务类型" />
          <el-table-column prop="attrs.BA1" label="提单号/柜号" />
          <el-table-column prop="totalCarton" label="总件数" />
          <el-table-column prop="totalCartonFedex" label="FEDEX件数" />
          <el-table-column prop="totalCartonUps" label="UPS件数" />
          <el-table-column prop="totalCartonTruck" label="卡派件数" />
          <el-table-column label="卡派明细" width="200">
            <template slot-scope="scope">
              <div v-if="scope.row.attrs.BA31" v-html="scope.row.attrs.BA31.replaceAll('\n','<br>')" />
            </template>
          </el-table-column>
          <el-table-column prop="totalCartonStockIn" label="留仓件数" />
          <el-table-column prop="totalWeight" label="总毛重" />
          <el-table-column prop="attrs.BA26" label="预约入仓时间" />
          <el-table-column prop="attrs.BA19" label="仓库" />
          <el-table-column prop="attrs.BA7" label="操作说明" />
          <el-table-column prop="attrs.BA27" label="到达仓库时间" />
          <el-table-column prop="attrs.BA28" label="拆柜时间" />
          <el-table-column prop="attrs.BA29" label="交柜时间" />
          <el-table-column prop="attrs.BA30" label="仓库备注" />
          <el-table-column label="附件下载" width="200px">
            <template slot-scope="scope">
              <el-button v-if="scope.row.attrs.BA11" size="mini" type="text" @click="downloadFile(scope.row.attrs.BA11)">提单下载</el-button>
              <el-button v-if="scope.row.attrs.BA12" size="mini" type="text" @click="downloadFile(scope.row.attrs.BA12)">清关资料下载</el-button>
              <el-button v-if="scope.row.attrs.BA13" size="mini" type="text" @click="downloadFile(scope.row.attrs.BA13)">物品描述下载</el-button>
              <el-button v-if="scope.row.attrs.BA14" size="mini" type="text" @click="downloadFile(scope.row.attrs.BA14)">ISF申报表下载</el-button>
              <el-button v-if="scope.row.attrs.BA15" size="mini" type="text" @click="downloadFile(scope.row.attrs.BA15)">FDRA文件下载</el-button>
              <el-button v-if="scope.row.attrs.BA16" size="mini" type="text" @click="downloadFile(scope.row.attrs.BA16)">卡派明细表下载</el-button>
            </template>
          </el-table-column>
          <el-table-column label="操作" fixed="right">
            <template slot-scope="scope">
              <el-dropdown style="margin-top:10px;">
                <el-button plain size="small">...</el-button>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item size="mini" type="text" @click.native="edit(scope.row)">
                    编辑
                  </el-dropdown-item>
                  <el-dropdown-item size="mini" type="text" @click.native="downloadDetail(scope.row)">
                    打印提单表
                  </el-dropdown-item>
                  <el-dropdown-item v-show="statusRadio !== '完成'" size="mini" type="text" @click.native="done(scope.row)">
                    完成
                  </el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </template>
          </el-table-column>
        </el-table>
      </el-main>
      <el-footer style="padding:0;background: #fff;">
        <div style="text-align: right; padding-top: 10px;">
          <el-pagination
            background
            :current-page="queryData.currentPage"
            :page-sizes="pageSizes"
            :page-size="queryData.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageTotal"
            style="float: right;"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </el-footer>
    </el-container>
    <!-- 新增批次-填写属性 -->
    <el-dialog title="编辑提单" :visible.sync="dialogAddVisible" :close-on-click-modal="false" top width="60%">
      <el-form class="edit-form" label-position="top">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="到达仓库时间">
              <el-date-picker v-model.lazy="formDataAttrs.BA27" type="datetime" placeholder="选择日期" value-format="yyyy-MM-dd HH:mm:ss" style="width:100%;" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="仓库">
              <el-input v-model.lazy="formDataAttrs.BA19" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="拆柜时间">
              <el-date-picker v-model.lazy="formDataAttrs.BA28" type="datetime" placeholder="选择日期" value-format="yyyy-MM-dd HH:mm:ss" style="width:100%;" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="交柜时间">
              <el-date-picker v-model.lazy="formDataAttrs.BA29" type="datetime" placeholder="选择日期" value-format="yyyy-MM-dd HH:mm:ss" style="width:100%;" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="仓库备注" style="">
              <el-input v-model.lazy="formDataAttrs.BA30" type="textarea" :rows="2" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogAddVisible = false">取 消</el-button>
        <el-button size="small" type="primary" @click="add">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapState } from 'vuex'
import request from '@/utils/request'
import { copyProperty, copyAllProperty } from '@/utils/tools'
import config from '@/settings'
import dateFormat from 'dateformat'
const BASE_URL = process.env.NODE_ENV === 'development' ? config.baseUrl.dev : config.baseUrl.pro
export default {
  data() {
    return {
        uploadUrl: BASE_URL + '/upload',
        statusRadio: '待处理',
        fileList: [],
        fileList2: [],
        fileList3: [],
        fileList4: [],
        fileList5: [],
        fileList6: [],
        table_body: [],
        pageTotal: 0,
        dialogAddVisible: false,
        multipleSelection: [],
        batchAttribute: '',
        formDataDefault: {
            batchId: '',
            type: '',
            serviceType: '',
            totalWeight: '',
            totalCartonFedex: '',
            totalCartonUps: '',
            totalCartonTruck: '',
            totalCartonStockIn: ''
        },
        formData: {
            batchId: '',
            type: '',
            serviceType: '',
            totalWeight: '',
            totalCartonFedex: '',
            totalCartonUps: '',
            totalCartonTruck: '',
            totalCartonStockIn: ''
        },
        formDataDefaultAttrs: {
            BA19: '',
            BA27: '',
            BA28: '',
            BA29: '',
            BA30: ''
      },
      formDataAttrs: {
             BA19: '',
            BA27: '',
            BA28: '',
            BA29: '',
            BA30: ''
        },
        endPlaceList: [
            'LAX',
            'JFK',
            'EWR',
            'LA',
            'LB',
            '其它'
        ],
      serviceTypeList: [],
      // 分页的数据
      pageSizes: [50, 100, 500],
      total: 0,
      types: [
          { id: 'AIR', name: '空运' },
          { id: 'SEA', name: '海运' },
          { id: 'GND', name: '铁运' }
      ],
      queryData: {
        pageSize: 50,
        currentPage: 1,
        status: '',
        stages: [4, 5, 6, 7]
      }
    }
  },
  computed: {
    tableHeight() {
      return document.documentElement.clientHeight - 290
    },
    ...mapState(//
      {
        customerOptions: state => state.base.customerOptions,
        profile: state => state.user
      }
    ),
    isOrgOwner() {
      return this.profile.orgOwner
    }
  },
  created() {
    this.initBatchServiceType()
    this.list()
  },
  methods: {
    downloadDetail(row) {
      window.open(BASE_URL + '/batch/download-detail?batchId=' + row.batchId)
    },
    list() {
      if (this.batchAttribute) {
        this.queryData.batchAttributes = this.batchAttribute.split(/,|\n|，/)
      } else {
        this.queryData.batchAttributes = []
      }
      request({
        url: 'batch/query',
        method: 'post',
        data: this.queryData
      }).then(res => {
        if (res.success) {
          if (res.data) {
            this.table_body = res.data.records
            this.pageTotal = res.data.total
          }
        }
      })
    },
    add() {
      const formDataTemp = {
        attrs: []
      }
      copyAllProperty(formDataTemp, this.formData, true)
      for (const key in this.formDataAttrs) {
        formDataTemp.attrs.push({ 'key': key, value: this.formDataAttrs[key] instanceof Date ? dateFormat(this.formDataAttrs[key], 'yyyy-mm-dd') : this.formDataAttrs[key] })
      }
      request({
        url: 'batch/add-attr',
        method: 'post',
        data: formDataTemp
      }).then(res => {
        if (res.success) {
          this.dialogAddVisible = false
          this.list()
          copyProperty(this.formData, this.formDataDefault)
          copyProperty(this.formDataAttrs, this.formDataDefaultAttrs)
        }
      })
    },
    edit(row) {
        copyProperty(this.formData, row)
        copyProperty(this.formDataAttrs, row.attrs)
        this.dialogAddVisible = true
    },
    statusChange(status) {
        switch (status) {
            case '待处理':
              this.queryData.stages = [4, 5, 6, 7]
              this.queryData.status = ''
              break
            case '完成':
              this.queryData.stages = [1]
              this.queryData.status = 'done'
              break
        }
        this.queryData.pageSize = 50
        this.queryData.currentPage = 1
        this.list()
    },
    done(row) {
        request({
        url: 'batch/done?batchId=' + row.batchId,
        method: 'get'
      }).then(res => {
        if (res.success) {
           this.list()
        }
      })
    },
    dones(row) {
      if (this.multipleSelection.length === 0) {
        this.$message({
          showClose: true,
          message: '请选择',
          type: 'error'
        })
        return
      }
      const batchIds = []
      this.multipleSelection.forEach(e => {
        batchIds.push(e.batchId)
      })
      request({
        url: 'batch/dones',
        method: 'post',
        data: { batchIds: batchIds }
      }).then(res => {
        if (res.success) {
           this.list()
        }
      })
    },
    uploadCallback1(res) {
        this.formDataAttrs.BA11 = res.ossKey
    },
    uploadCallback2(res) {
        this.formDataAttrs.BA12 = res.ossKey
    },
    uploadCallback3(res) {
        this.formDataAttrs.BA13 = res.ossKey
    },
    uploadCallback4(res) {
        this.formDataAttrs.BA14 = res.ossKey
    },
    uploadCallback5(res) {
        this.formDataAttrs.BA15 = res.ossKey
    },
    uploadCallback6(res) {
        this.formDataAttrs.BA16 = res.ossKey
    },
      // 分页
    handleSizeChange(val) {
      this.queryData.pageSize = val
      this.queryData.currentPage = 1
      this.list()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.list()
    },
    initBatchServiceType(response) {
      request({
        url: 'batch/service-type/option',
        method: 'get'
      }).then(res => {
        if (res.success) {
            this.serviceTypeList = res.data
        }
      })
    },
    exportOrderCost() {
    },
    clearFileList() {
        // 清空
        this.fileList = []
        this.fileList2 = []
        this.fileList3 = []
        this.fileList4 = []
        this.fileList5 = []
        this.fileList6 = []
    },
    downloadFile(ossKey) {
       request({
        url: 'download?ossKey=' + ossKey,
        method: 'get'
      }).then(res => {
        if (res.success) {
            window.open(res.data)
        }
      })
    },
    showAddDialog() {
        this.dialogAddVisible = true
        this.queryData.currentPage = 1
        this.queryData.pageSize = this.pageSizes[0]
    },
    // 多选按钮
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleRemove(file, fileList) {
      this.fileList = []
    },
    handleRemove2(file, fileList) {
      this.fileList2 = []
    },
    handleRemove3(file, fileList) {
      this.fileList3 = []
    },
    handleRemove4(file, fileList) {
      this.fileList4 = []
    },
    handleRemove5(file, fileList) {
      this.fileList5 = []
    },
    handleRemove6(file, fileList) {
      this.fileList6 = []
    }
  }
}
</script>
